<?php
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$queue_zlist_fetch_key = 'Queue_zcopy_images_fetch';
$mh_list_ids = 'Running_zcopy_images_to_queue';

$model = new Model;
$http  = new Http;
$cut   = new Cut;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||queue_mh_zcopy_images');

$limit = 100;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    break;
  }

  $lsize = $redis->lSize($queue_zlist_fetch_key);
  if($lsize > 100) {
    break;
  }
  
  $result = array();
  $where = "list_state=0 AND state in(0,1) ";
  
  $list = $model->query('select * from news_zcopy where '.$where.' order by news_state desc limit 50');
  if($list) {
    foreach ($list as $info) {
      $queue_data = array(
        'id' => $info['id'],
        'state' => $info['state'],
        'link' => $info['link'],
        'title' => $info['title'],
        'chapter_count' => $info['chapter_count'],
        'chapter' => $info['chapter'],
        'info' => $info['info'],
        'status' => $info['status'],
        'list_state' => 9,
        'news_state' => $info['news_state'],
      );

      $redis->rPush($queue_zlist_fetch_key, serialize($queue_data));

      $update_data = array('list_state'=>9);
      $model->save("news_zcopy", "id='$info[id]'", $update_data);
      //print_r($queue_data);
    }
  }

  sleep(2);
}

$model->close();
$redis->hDel(MH_PIDS, $pid);